﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;

namespace SCOSub
{

    class Const
    {
      //  public const string ver = "SCOSub 忘忧字幕组内部工具" + Assembly.GetExecutingAssembly().ImageRuntimeVersion;

        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk);
        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        //需要初始化的一些常量
        public static int Address = 0;
        public static int MaxLen = 24;
        public static int MinLen = 0;
        public static int AutoSaveSpan = 5;//自动保存
        public static int MinLastTime = 500;
        public static int FFMS2VideoProviderFrameBuffer = 24;
        public static int HotKeyID = 113;
        public static bool DoTopMost = true;
        public static AutoOffsetMode autooffsetmode = AutoOffsetMode.LuoMa;
        public static IntPtr MainFormHWND = IntPtr.Zero;


        public readonly static Hashtable effectslist = new Hashtable();
        /// <summary>
        /// 初始化特效列表
        /// </summary>
        public static void InitEffectsList()
        {
            effectslist.Add("conans专用,无覆盖", "conansPrivateWithoutOverlay");
            effectslist.Add("二分时间段", "HalfTime");
            effectslist.Add("带fad的卡拉OK", "FadeKaraoKe");
            effectslist.Add("基本卡拉OK", "BasicKaraoKe");
            effectslist.Add("conans专用", "conansPrivate");
            
       }

  

        #region xml
       
        public static void LoadConfig()//加载设置xml,xpath
        {
            XmlReader xr = null;
            Version configver;
            try
            {
                xr = XmlReader.Create(System.Windows.Forms.Application.StartupPath + "\\config.xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xr);
                configver = new Version(ReadXmlAsString(xmldoc, @"/Config/ConfigVer"));
                if (configver != new Version(Application.ProductVersion))
                {
                    
                    throw new Exception();
                }
                Address = ReadXmlAsInt32(xmldoc, @"/Config/MemoryAddress");
                MaxLen = ReadXmlAsInt32(xmldoc, @"/Config/MaxLen");
                MinLen = ReadXmlAsInt32(xmldoc, @"/Config/MinLen");
                AutoSaveSpan = ReadXmlAsInt32(xmldoc, @"/Config/AutoSaveSpan");
                MinLastTime = ReadXmlAsInt32(xmldoc, @"/Config/MinLastTime");
                DoTopMost = ("True" == ReadXmlAsString(xmldoc, @"/Config/DoTopMost"));
                FFMS2VideoProviderFrameBuffer = ReadXmlAsInt32(xmldoc, @"/Config/FFMS2VideoProviderFrameBuffer");
                HotKeyID = ReadXmlAsInt32(xmldoc, @"/Config/HotKey");
                autooffsetmode = (AutoOffsetMode)ReadXmlAsInt32(xmldoc, @"/Config/AutoOffsetMode");
                
                xmldoc = null;
                xr.Close();
                xr = null;
            

            }
            catch
            {
                if (xr != null)
                    xr.Close();
                SaveConfig();
            }
            finally
            {
                if (xr != null && xr.ReadState != ReadState.Closed)
                {
                    xr.Close();
                    xr = null;
                }
            }
            UnregisterHotKey(MainFormHWND, 200);
            RegisterHotKey(MainFormHWND, 200, 0, (Keys)HotKeyID);
           
        }
             
        public static void SaveConfig()//保存设置
        {
            XmlWriterSettings xmlWS = new XmlWriterSettings();
            xmlWS.CheckCharacters = true;
            xmlWS.Indent = true;
            xmlWS.Encoding = Encoding.UTF8;
            XmlWriter xw = XmlWriter.Create(System.Windows.Forms.Application.StartupPath + "\\config.xml", xmlWS);
            xw.WriteStartDocument();
            xw.WriteStartElement("Config");
            xw.WriteElementString("ConfigVer", Application.ProductVersion);
            xw.WriteElementString("MemoryAddress", Address.ToString());
            xw.WriteElementString("MaxLen", MaxLen.ToString());
            xw.WriteElementString("MinLen", MinLen.ToString());
            xw.WriteElementString("AutoSaveSpan", AutoSaveSpan.ToString());
            xw.WriteElementString("MinLastTime", MinLastTime.ToString());
            xw.WriteElementString("DoTopMost", DoTopMost.ToString());
            xw.WriteElementString("FFMS2VideoProviderFrameBuffer", FFMS2VideoProviderFrameBuffer.ToString());
            xw.WriteElementString("HotKey", HotKeyID.ToString());
            xw.WriteElementString("AutoOffsetMode", ((int)autooffsetmode).ToString());
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
            UnregisterHotKey(MainFormHWND, 200);
            RegisterHotKey(MainFormHWND, 200, 0, (Keys)HotKeyID);
        }
        private static int ReadXmlAsInt32(XmlDocument xmldoc, string xpath)
        {
            try
            {
                return int.Parse(xmldoc.SelectSingleNode(xpath).InnerText);
            }
            catch
            {
                return -1;
            }
        }
        private static string ReadXmlAsString(XmlDocument xmldoc, string xpath)
        {
            return xmldoc.SelectSingleNode(xpath).InnerText;
        }

        #endregion


    }
    enum AutoOffsetMode
    {
        One = 1,
        Two = 2,
        Space = 3,
        LuoMa = 4
    }
}
